package lab3_2;

import java.util.Random;

public abstract class BinaryOperation {
    protected int leftOperand;
    protected int rightOperand;
    protected char operator;
    protected int value;
    protected static final int UPPER = 100;
    protected static final int LOWER = 0;
    protected static Random random = new Random();
    
    public BinaryOperation() {}
    
    public BinaryOperation(int left, int right, char op) {
        this.leftOperand = left;
        this.rightOperand = right;
        this.operator = op;
        this.value = calculate(left, right);
    }
    
    // 抽象方法
    public abstract int calculate(int left, int right);
    public abstract boolean checkingCalculation(int left, int right);
    
    // 访问器方法
    public int getLeftOperand() {
        return leftOperand;
    }
    
    public int getRightOperand() {
        return rightOperand;
    }
    
    public char getOperator() {
        return operator;
    }
    
    public int getValue() {
        return value;
    }
    
    // 生成指定运算符的算式
    public static BinaryOperation generateOperation(char anOperator) {
        if (anOperator == '+') {
            return AdditionOperation.generateAdditionOperation();
        } else if (anOperator == '-') {
            return SubstractOperation.generateSubstractOperation();
        } else {
            throw new IllegalArgumentException("不支持的运算符: " + anOperator);
        }
    }
    
    // 随机生成加法或减法算式
    public static BinaryOperation generateBinaryOperation() {
        if (random.nextBoolean()) {
            return AdditionOperation.generateAdditionOperation();
        } else {
            return SubstractOperation.generateSubstractOperation();
        }
    }
    
    // 重写equals方法
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        
        BinaryOperation that = (BinaryOperation) obj;
        return leftOperand == that.leftOperand &&
               rightOperand == that.rightOperand &&
               operator == that.operator;
    }
    
    // 字符串表示
    @Override
    public String toString() {
        return leftOperand + "" + operator + "" + rightOperand;
    }
    
    public String asString() {
        return toString();
    }
    
    public String fullString() {
        return toString() + "=" + value;
    }
}